<template>
	<view class="my-page">
		<view class="user-header">
			<view class="avatar-container">
				<image class="avatar" :src="avatarUrl" @click="chooseAvatar"></image>
				<text class="avatar-tip">点击更换头像</text>
			</view>
			<view class="user-info">
				<text class="welcome">HI~欢迎回来</text>
				<button class="login-btn" v-if="!isLogin" @click="goToLoginPage">登录</button>
				<text class="username" v-if="isLogin">{{ username }}</text>
			</view>
		</view>

		<view class="stats-bar" v-if="isLogin">
			<text class="stats-item">关注3</text>
			<text class="stats-split">|</text>
			<text class="stats-item">话题0</text>
			<text class="stats-split">|</text>
			<text class="logout-link" @click="handleLogout">退出</text>
		</view>
		<view class="stats-bar" v-else>
			<text class="stats-item">关注3</text>
			<text class="stats-split">|</text>
			<text class="stats-item">话题0</text>
		</view>

		<view class="func-btns">
			<button class="func-btn" @click="toCollect" :disabled="!isLogin">
				<image src="/static/icon_star.png"></image>
				<text>收藏</text>
			</button>
			<button class="func-btn" @click="toHistory" :disabled="!isLogin">
				<image src="/static/icon_clock.png"></image>
				<text>历史</text>
			</button>
			<button class="func-btn" @click="toSetting" :disabled="!isLogin">
				<image src="/static/icon_setting.png"></image>
				<text>设置</text>
			</button>
		</view>

		<!-- 红包广告横幅 -->
		<view class="ad-banner">
			<image src="/static/ad_redpacket.png"></image>
		</view>

		<!-- 发布引导区 -->
		<view class="publish-guide">
			<view class="icons">
				<image src="/static/icon_video.png"></image>
				<image src="/static/icon_pic.png"></image>
				<image src="/static/icon_link.png"></image>
			</view>
			<text>已有385,893人在这里发布身边的新鲜事</text>
			<button class="publish-btn" @click="toPublish" :disabled="!isLogin">我也要发布</button>
			<text class="link" @click="toBrowse">先去逛逛></text>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				isLogin: false, 
				username: '', 
				avatarUrl: '/static/default-avatar.png' 
			}
		},
		onShow() {
			const account = uni.getStorageSync('Account')
			this.isLogin = !!account
			this.username = account || ''
			this.avatarUrl = uni.getStorageSync('AvatarUrl') || '/static/default-avatar.png'

			uni.setTabBarItem({
				index: 3,
				text: this.isLogin ? '我的' : '未登录'
			})
		},
		methods: {
			goToLoginPage() {
				uni.navigateTo({
					url: '/pages/components/login/login'
				})
			},
			handleLogout() {
				uni.showModal({
					title: '确认退出',
					content: '确定要退出当前账号吗？',
					success: (res) => {
						if (res.confirm) {
							uni.clearStorageSync('Account')
							uni.clearStorageSync('AvatarUrl')
							this.isLogin = false
							this.username = ''
							uni.setTabBarItem({
								index: 3,
								text: '未登录'
							})
							uni.showToast({
								title: '已退出登录',
								icon: 'success'
							})
						}
					}
				})
			},
			chooseAvatar() {
				if (!this.isLogin) {
					return uni.showToast({
						title: '请先登录',
						icon: 'none'
					})
				}
				uni.chooseImage({
					count: 1,
					sizeType: ['original', 'compressed'],
					sourceType: ['album', 'camera'],
					success: (res) => {
						this.avatarUrl = res.tempFilePaths[0]
						uni.setStorageSync('AvatarUrl', this.avatarUrl)
					},
					fail: (err) => {
						console.error('选择头像失败', err)
						uni.showToast({
							title: '选择头像失败',
							icon: 'none'
						})
					}
				})
			},
			toCollect() {
				uni.showToast({
					title: '进入收藏页',
					icon: 'none'
				})
			},
			toHistory() {
				uni.showToast({
					title: '进入历史页',
					icon: 'none'
				})
			},
			toSetting() {
				uni.showToast({
					title: '进入设置页',
					icon: 'none'
				})
			},
			toPublish() {
				uni.showToast({
					title: '进入发布页',
					icon: 'none'
				})
			},
			toBrowse() {
				uni.showToast({
					title: '去逛逛~',
					icon: 'none'
				})
			}
		}
	}
</script>

<style scoped>
	.my-page {
		padding: 0;
		min-height: 100vh;
		background-color: #ffffff;
	}

	.user-header {
		background: linear-gradient(90deg, #ffc0cb 0%, #db7093 100%);
		color: #fff;
		padding: 25rpx 30rpx;
		display: flex;
		align-items: center;
		gap: 20rpx;
	}

	.avatar-container {
		text-align: center;
	}
	.avatar {
		width: 120rpx;
		height: 120rpx;
		border-radius: 50%;
		border: 2rpx solid #eee;
		margin-bottom: 8rpx;
	}
	.avatar-tip {
		font-size: 20rpx;
		color: #fff;
		display: block;
	}

	.user-info {
		flex: 1; 
	}
	.welcome {
		font-size: 28rpx;
		display: block;
	}
	.login-btn {
		background-color: #ffaad5;
		color: #fff;
		border-radius: 6rpx;
		padding: 8rpx 20rpx;
		font-size: 24rpx;
		margin-top: 8rpx;
		border: none;
	}
	.username {
		font-size: 32rpx;
		font-weight: 600;
		margin-top: 8rpx;
		display: block;
	}

	.stats-bar {
		background: linear-gradient(90deg, #ffc0cb 0%, #db7093 100%);
		color: #fff;
		padding: 15rpx 30rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		gap: 15rpx;
		font-size: 28rpx;
	}
	.stats-item {
		opacity: 0.9;
	}
	.stats-split {
		opacity: 0.7;
	}
	.logout-link {
		font-size: 26rpx;
		opacity: 0.9;
		text-decoration: underline; 
		padding: 5rpx 10rpx; 
	}
	.logout-link:active {
		opacity: 0.7; 
	}

	/* 功能按钮区 */
	.func-btns {
		display: flex;
		justify-content: space-around;
		padding: 20rpx 0;
		background: #fff;
		border-bottom: 1px solid #f0f0f0;
	}
	.func-btn {
		display: flex;
		flex-direction: column;
		align-items: center;
		background: transparent;
		color: #333;
		border: none;
	}
	.func-btn image {
		width: 40rpx;
		height: 40rpx;
		margin-bottom: 8rpx;
		filter: invert(22%) sepia(91%) saturate(1668%) hue-rotate(345deg) brightness(94%) contrast(99%);
	}
	.func-btn text {
		font-size: 24rpx;
	}
	.func-btn:disabled {
		color: #999;
	}
	.func-btn:disabled image {
		filter: grayscale(100%);
	}

	/* 广告横幅 */
	.ad-banner {
		padding: 15rpx;
		background: #fff;
	}
	.ad-banner image {
		width: 100%;
		height: auto;
		border-radius: 8rpx;
	}

	/* 发布引导区 */
	.publish-guide {
		padding: 40rpx 30rpx;
		background: #fff;
		text-align: center;
	}
	.icons {
		display: flex;
		justify-content: center;
		gap: 40rpx;
		margin-bottom: 20rpx;
	}
	.icons image {
		width: 60rpx;
		height: 60rpx;
		background: #f8f8f8;
		padding: 10rpx;
		border-radius: 10rpx;
	}
	.publish-guide text {
		font-size: 26rpx;
		color: #666;
		display: block;
	}
	.publish-btn {
		background: #f47983;
		color: #fff;
		border-radius: 30rpx;
		padding: 15rpx 40rpx;
		margin: 20rpx 0;
		font-size: 28rpx;
		border: none;
	}
	.publish-btn:disabled {
		background: #ccc;
		color: #fff;
	}
	.link {
		color: #007aff;
		font-size: 24rpx;
		text-decoration: underline;
	}
</style>